package com.uzhie.web.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.io.*;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import com.uzhie.util.ConstantKit.*;

/**
 * 配置信息编辑服务
 * Created by lilongfei on 2018/7/19.
 */
@Service
public class ConfigureService {

    /**
     * 读取平台的所有配置信息
     *
     * @return
     */
    public JSONObject readProperties() throws IOException {
        Properties properties = new Properties();
        // 使用InPutStream流读取properties文件
        BufferedReader bufferedReader = null;

        String path = getClass().getResource("/sys.properties").getPath();
        bufferedReader = new BufferedReader(new FileReader(path));
        properties.load(bufferedReader);

        // 拼装流媒体服务器设置
        JSONObject media = new JSONObject();
        media.put(MEDIA.URL.N(), properties.getProperty(MEDIA.URL.N()));
        media.put(MEDIA.IP.N(), properties.getProperty(MEDIA.IP.N()));
        media.put(MEDIA.TCP.N(), properties.getProperty(MEDIA.TCP.N()));
        media.put(MEDIA.TCP_REPLAY.N(), properties.getProperty(MEDIA.TCP_REPLAY.N()));
        media.put(MEDIA.VIDEO_TCP_LIVE.N(), properties.getProperty(MEDIA.VIDEO_TCP_LIVE.N()));
        media.put(MEDIA.VIDEO_TCP_REPLAY.N(), properties.getProperty(MEDIA.VIDEO_TCP_REPLAY.N()));
        media.put(MEDIA.VIDEO_DOWN.N(), properties.getProperty(MEDIA.VIDEO_DOWN.N()));


        media.put(MEDIA.UDP.N(), properties.getProperty(MEDIA.UDP.N()));
        media.put(MEDIA.OFFTIME.N(), properties.getProperty(MEDIA.OFFTIME.N()));
        media.put(MEDIA.RECONNECT.N(), properties.getProperty(MEDIA.RECONNECT.N()));
        media.put(MEDIA.UP.N(), properties.getProperty(MEDIA.UP.N()));
        media.put(MEDIA.DOWN.N(), properties.getProperty(MEDIA.DOWN.N()));
        //拼装上传服务器设置
        JSONObject upload = new JSONObject();
        upload.put(UPLOAD.URL.N(), properties.getProperty(UPLOAD.URL.N()));
        upload.put(UPLOAD.PORT.N(), properties.getProperty(UPLOAD.PORT.N()));
        upload.put(UPLOAD.MEDIA.N(), properties.getProperty(UPLOAD.MEDIA.N()));
        upload.put(UPLOAD.AUDIO.N(), properties.getProperty(UPLOAD.AUDIO.N()));
        upload.put(UPLOAD.PIC.N(), properties.getProperty(UPLOAD.PIC.N()));
        upload.put(UPLOAD.USER.N(), properties.getProperty(UPLOAD.USER.N()));
        upload.put(UPLOAD.PWD.N(), properties.getProperty(UPLOAD.PWD.N()));
        //拼装上级平台设置
        JSONObject level = new JSONObject();
        level.put(LEVEL.CODE.N(), properties.getProperty(LEVEL.CODE.N()));
        level.put(LEVEL.TIME.N(), properties.getProperty(LEVEL.TIME.N()));
        //拼装返回结构
        JSONObject result = new JSONObject();
        result.put(MEDIA.getName(), media);
        result.put(UPLOAD.getName(), upload);
        result.put(LEVEL.getName(), level);

        //关闭读写流
        bufferedReader.close();

        return result;
    }

    /**
     * 写入配置文件信息
     *
     * @param value
     */
    public void writeProperties(@NotNull(message = "配置文件信息不可为空") Map<String, Map<String, String>> value) throws IOException {
        Properties prop = new Properties();
        String path = getClass().getResource("/sys.properties").getPath();
        File file = new File(path);
        if (file.exists()) file.delete();
        FileOutputStream oFile = null;//true表示追加打开
        oFile = new FileOutputStream(path, true);
        Set<Map.Entry<String, Map<String, String>>> set = value.entrySet();
        Iterator<Map.Entry<String, Map<String, String>>> iterator = set.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Map<String, String>> entry = iterator.next();
            Set<Map.Entry<String, String>> entrySet = entry.getValue().entrySet();
            Iterator<Map.Entry<String, String>> entryIterator = entrySet.iterator();
            while (entryIterator.hasNext()) {
                Map.Entry<String, String> stringEntry = entryIterator.next();
                prop.put(stringEntry.getKey(), stringEntry.getValue());
            }
        }

        prop.store(oFile, "服务器配置文件");
        oFile.close();

    }
}
